package com.example.supernote.ui.settings

import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat.startActivity
import androidx.core.os.LocaleListCompat
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import com.example.supernote.model.settings.ModeType
import com.example.supernote.model.settings.LanguageType
import com.example.supernote.ui.MainActivity
import com.example.supernote.ui.theme.DarkColors
import com.example.supernote.ui.theme.LightColors
import com.example.supernote.ui.theme.SuperNoteShapes
import com.example.supernote.ui.theme.SuperNoteTheme
import com.example.supernote.ui.theme.SuperNoteTypography

val ModeData = mutableListOf(
//    ModeType.Light,
//    ModeType.Dark,
    ModeType.FollowSystem
)

val LanguageData = mutableListOf(
//    LanguageType.Chinese,
//    LanguageType.English,
    LanguageType.FollowSystem
)

data class SettingsUiState(
    val selectModel : ModeType = ModeType.Light,
    val selectLanguage : LanguageType = LanguageType.Chinese
)


class SettingsViewModel():ViewModel(){
    private val _uiState = MutableStateFlow(SettingsUiState())
    val uiState: StateFlow<SettingsUiState> = _uiState.asStateFlow()

    init {
        viewModelScope.launch {
            _uiState.update { it.copy(selectModel = ModeType.Light, selectLanguage = LanguageType.Chinese) }
        }
    }

    fun changeModel(selectModel: ModeType){
        _uiState.update { it.copy(selectModel=selectModel) }
    }
    fun changeLanguage(selectLanguage: LanguageType, context:Context){
        _uiState.update { it.copy(selectLanguage=selectLanguage) }
//        when (selectLanguage) {
//            LanguageType.English -> {
////                AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags("en-rUS"))
//                println("here")
//                shift.shiftLanguage("en",context)
//            }
//            LanguageType.Chinese -> {
//                AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags("zh"))
//            }
//            // 处理其他语言
//            else->{
//
//            }
//        }

        // 重启应用以使更改生效
//        restartApp(context)
    }

    private fun restartApp(context: Context) {
        val intent = Intent(context, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
    }

    companion object {
        fun provideFactory(
        ): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
            @Suppress("UNCHECKED_CAST")
            override fun <T : ViewModel> create(modelClass: Class<T>): T {
                return SettingsViewModel() as T
            }
        }
    }
}